package com.shop.manager.controller;

//import com.shop.manager.entity.User;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.User;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 用户增删改查
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private UserService userService;

    /**
     * @Description 添加用户
     * @param user 用户对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) User user){
        try {
            userService.add(user);
            return HttpResult.ok("添加用户成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加用户失败!");
    }

    /**
     * @Description 批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Long[] ids){
        try {
            userService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改用户
     * @param user 用户对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) User user){
        try {
            userService.update(user);
            return HttpResult.ok("修改用户成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改用户失败!");
    }

    /**
     * @Description 根据ID查询用户
     * @param id 用户id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id){
        return HttpResult.ok(userService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param user 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) User user){
        try {
            //分页查询
            PageResult result = userService.findPage(pageNum, size, user);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error();
        }
    }
}
